package com.obook.bln;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class BLNCommand {

    boolean debug = false;

    private static final String TAG = BLNCommand.class.getSimpleName();

    public static final String CMD_OPEN_GREEN = "echo 0 > /sys/devices/platform/leds-gpio/leds/green/brightness \n";
    public static final String CMD_CLOSE_GREEN = "echo 1 > /sys/devices/platform/leds-gpio/leds/green/brightness \n";

    public static final String CMD_OPEN_RED = "echo 0 > /sys/devices/platform/leds-gpio/leds/red/brightness \n";
    public static final String CMD_CLOSE_RED = "echo 1 > /sys/devices/platform/leds-gpio/leds/red/brightness \n";

    public static final String CMD_CHANGE_BRIGHTNESS_PATTERN = "echo %d > /sys/class/misc/misc_dev/pwm_leds/green_func \n";

    public static final String CMD_OPEN_MATOR = "echo 0 > /sys/devices/platform/leds-gpio/leds/vibrator/brightness \n";
    public static final String CMD_CLOSE_MATOR = "echo 1 > /sys/devices/platform/leds-gpio/leds/vibrator/brightness \n";

    public static final int MAX_BRIGHTNESS = 195;
    public static final int MIN_BRIGHTNESS = 101;

    private OutputStream os;

    public void exec(String cmd) {

        try {
            Process process = Runtime.getRuntime().exec("/system/bin/sh");
            InputStream ins = process.getErrorStream();
            os = process.getOutputStream();
            os.write(cmd.getBytes());
            os.write("exit\n".getBytes());
            os.flush();

            if (debug) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int i = 0;
                while ((i = ins.read()) != -1) {
                    baos.write(i);
                }

                Log.d(TAG, "exec: error MSG = " + baos.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
